package com.example.podcaster.services;

import java.io.File;
import java.io.Serializable;
import java.net.URL;

public class DownloadTask implements Serializable
{
    private static final long serialVersionUID = -4265807526591862074L;

    public enum State
    {
        COMPLETE, ERROR, CANCELLED, DOWNLOADING, PAUSED
    }

    private long totalSize = -1;

    private long downloadedSize = -1;

    private boolean terminated = false;

    private State state = State.PAUSED;

    private final int id;

    private final URL url;

    private final File file;

    public DownloadTask(int id, URL url, File file)
    {
        this.id = id;
        this.url = url;
        this.file = file;
    }

    public synchronized void setTotalSize(long totalSize)
    {
        this.totalSize = totalSize;
    }

    public synchronized void setDownloadedSize(long downloadedSize)
    {
        this.downloadedSize = downloadedSize;
    }

    public synchronized void download()
    {
    	if (state == State.PAUSED)
    	{
    		this.state = State.DOWNLOADING;
    	}
    }

    public synchronized void pause()
    {
    	if (state == State.DOWNLOADING)
    	{
    		this.state = State.PAUSED;
    	}
    }

    public synchronized void complete()
    {
        terminated = true;
        state = State.COMPLETE;
    }

    public synchronized void cancel()
    {
        terminated = true;
        state = State.CANCELLED;
    }

    public synchronized void error()
    {
        terminated = true;
        state = State.ERROR;
    }

    public synchronized long getTotalSize()
    {
        return totalSize;
    }

    public synchronized long getDownloadedSize()
    {
        return downloadedSize;
    }

    public synchronized boolean isTerminated()
    {
        return terminated;
    }

    public synchronized boolean isCancelled()
    {
        return state == State.CANCELLED;
    }

    public synchronized State getState()
    {
        return state;
    }

    public synchronized int getId()
    {
        return id;
    }

    public synchronized URL getUrl()
    {
        return url;
    }

    public synchronized File getFile()
    {
        return file;
    }

    public synchronized boolean isDownloading()
    {
        return this.state == State.DOWNLOADING;
    }
}
